﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;
using Windows.Storage;
using TopGroup.MySharePoint.Data;

// La plantilla de elemento Control de usuario está documentada en http://go.microsoft.com/fwlink/?LinkId=234236

namespace TopGroup.MySharePoint
{
    public partial class SettingsUserControl : UserControl
    {
        App app = (TopGroup.MySharePoint.App)App.Current;
        private string error = string.Empty;
        public string Error
        {
            set { error = value; }
            get { return error; }
        }
        public async void ShowError(string error)
        {
            if (!string.IsNullOrEmpty(error))
            {
                var dlg = new Windows.UI.Popups.MessageDialog(error);
                var temp = await dlg.ShowAsync();
            }
        }
        public SettingsUserControl()
        {
            this.InitializeComponent();
            if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("url"))
                url.Text = (string)ApplicationData.Current.RoamingSettings.Values["url"];
            if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("login"))
                login.Text = (string)ApplicationData.Current.RoamingSettings.Values["login"];
            if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("password"))
                password.Password = (string)ApplicationData.Current.RoamingSettings.Values["password"];
            if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("dominio"))
                dominio.Text = (string)ApplicationData.Current.RoamingSettings.Values["dominio"];
            if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("listName"))
                listName.Text = (string)ApplicationData.Current.RoamingSettings.Values["listName"];
            if (ApplicationData.Current.RoamingSettings.Values.ContainsKey("contacts"))
                contacts.Text = (string)ApplicationData.Current.RoamingSettings.Values["contacts"];
           
        }
        public void Actualizar_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(url.Text))
                ShowError("\nThe url field can not be empty. Please go to settings to assign a value.");
            else if (string.IsNullOrEmpty(login.Text))
                ShowError("\nThe login field can not be empty. Please go to settings to assign a value.");
            else if (string.IsNullOrEmpty(password.Password))
                ShowError("\nThe password field can not be empty. Please go to settings to assign a value.");
            else if (string.IsNullOrEmpty(dominio.Text))
                ShowError("\nThe domain field can not be empty. Please go to settings to assign a value.");
            else if (string.IsNullOrEmpty(listName.Text))
                ShowError("\nThe list name field can not be empty. Please go to settings to assign a value and verify to match with name in your SharePoint Site.");
            else if (string.IsNullOrEmpty(contacts.Text))
                ShowError("\nThe contacts field can not be empty. Please go to settings to assign a value.");
            else
            {
                ApplicationData.Current.RoamingSettings.Values["url"] = url.Text;
                ApplicationData.Current.RoamingSettings.Values["login"] = login.Text;
                ApplicationData.Current.RoamingSettings.Values["password"] = password.Password;
                ApplicationData.Current.RoamingSettings.Values["dominio"] = dominio.Text;
                ApplicationData.Current.RoamingSettings.Values["listName"] = listName.Text;
                ApplicationData.Current.RoamingSettings.Values["contacts"] = contacts.Text;

                SampleDataSource.Reload();
                GroupedItemsPage rootPage = GroupedItemsPage.Current;
                rootPage.Actualizartile();
                rootPage.LoadFromSettings();
            }
        }

        
    }
}


